#pragma once
#include "Hash.h"



namespace zsw
{
	template<class K>
	struct Hash
	{
		size_t operator()(const K& key)
		{
			return key;
		}
	};

	template<>
	struct Hash<string>
	{
		size_t operator()(const string& key)
		{
			size_t hashi = 0;
			for (auto c : key)
			{
				hashi += c;
				hashi *= 31;
			}
			return hashi;
		}
	};

	template<class K, class V, class Hash = Hash<K>>
	class map
	{
	public:
		struct MapKeyOfT
		{
			K operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};

		typedef typename HashTables<K, pair<K, V>, Hash, MapKeyOfT>::iterator iterator;
		typedef typename HashTables<K, pair<K, V>, Hash, MapKeyOfT>::const_iterator const_iterator;

		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		const_iterator begin() const
		{
			return _ht.begin();
		}

		const_iterator end() const
		{
			return _ht.end();
		}

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _ht.insert(kv);
		}

		bool erase(const K& key)
		{
			return _ht.erase(key);
		}

		iterator find(const K& key)
		{
			return _ht.find(key);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = _ht.insert(make_pair(key, V()));
			return (ret.first)._pnode->_data.second;
		}

	private:
		HashTables<K, pair<K, V>, Hash, MapKeyOfT> _ht;
	};
}